Skip to content

fix(serializers): prevent AttributeError and ObjectDoesNotExist in PluginConfigSerializer secret redaction. Closes #4052 - #4053

Open
harshitnub077 wants to merge 1 commit into
intelowlproject:developfrom
harshitnub077:fix/plugin-config-secret-redaction-null-owner
Open

harshitnub077 wants to merge 1 commit into
intelowlproject:developfrom
harshitnub077:fix/plugin-config-secret-redaction-null-owner

Conversation

@harshitnub077

@harshitnub077 harshitnub077 commented Oct 3, 2026 •

Copy link
Copy Markdown

Description

When serializing a PluginConfig with is_secret=True and for_organization=True, PluginConfigSerializer.CustomValueField.get_attribute directly evaluated instance.owner.pk and instance.owner.membership.organization.pk.

This led to unhandled 500 exceptions in two scenarios:

  1. AttributeError: 'NoneType' object has no attribute 'pk' when instance.owner is None (which is supported in the database constraints under api_app/models.py).
  2. ObjectDoesNotExist: User has no membership when instance.owner is set but lacks an active membership (e.g. users removed from an organization after the config was saved).
  3. Potential issues if request or an authenticated user is missing from serializer context.

This PR adds safe guards in PluginConfigSerializer.CustomValueField.get_attribute (api_app/serializers/plugin.py):

  • Checks user.is_authenticated and instance.owner is not None before comparing user.pk == instance.owner.pk.
  • Validates user.has_membership(), user.membership.is_admin, instance.owner.has_membership(), and compares organization_id directly using the FK column — avoiding any .membership attribute traversal that can raise RelatedObjectDoesNotExist.
  • Safely redacts secrets for unauthorized users and when owner / membership is absent.
  • Adds three unit tests in PluginConfigSerializerTestCase covering: global null-owner secrets, post-creation membership removal, and cross-organization visibility.

Note on test fixtures: The initial tests attempted to create PluginConfig(owner=None, for_organization=True) and PluginConfig(owner=user_without_membership, for_organization=True). Both are forbidden at the DB level by clean_for_organization() in api_app/interfaces.py. The tests were corrected to cover the real runtime edge cases: a valid owner=None global secret (exercises the null-owner guard without hitting the DB constraint), and a membership deleted after the config was saved (the actual scenario that triggered ObjectDoesNotExist).

Closes #4052

Type of change

  • Bug fix (non-breaking change which fixes an issue).

Checklist

  • I have read and understood the rules about how to Contribute to this project
  • The pull request is for the branch develop
  • A new plugin (analyzer, connector, visualizer, playbook, pivot or ingestor) was added or changed, in which case:
    • I strictly followed the documentation "How to create a Plugin"
    • Usage file was updated. A link to the PR to the docs repo has been added as a comment here.
    • Advanced-Usage was updated (in case the plugin provides additional optional configuration). A link to the PR to the docs repo has been added as a comment here.
    • I have dumped the configuration from Django Admin using the dumpplugin command and added it in the project as a data migration. ("How to share a plugin with the community")
    • If a File analyzer was added and it supports a mimetype which is not already supported, you added a sample of that type inside the archive test_files.zip and you added the default tests for that mimetype in test_classes.py.
    • If you created a new analyzer and it is free (does not require any API key), please add it in the FREE_TO_USE_ANALYZERS playbook by following this guide.
    • Check if it could make sense to add that analyzer/connector to other freely available playbooks.
    • I have provided the resulting raw JSON of a finished analysis and a screenshot of the results.
    • If the plugin interacts with an external service, I have created an attribute called precisely url that contains this information. This is required for Health Checks (HEAD HTTP requests).
    • If a new analyzer has beed added, I have created a unittest for it in the appropriate dir. I have also mocked all the external calls, so that no real calls are being made while testing.
    • I have added that raw JSON sample to the get_mocker_response() method of the unittest class. This serves us to provide a valid sample for testing.
    • I have created the corresponding DataModel for the new analyzer following the documentation
  • I have inserted the copyright banner at the start of the file: # This file is a part of IntelOwl https://github.com/intelowlproject/IntelOwl # See the file 'LICENSE' for copying permission.
  • Please avoid adding new libraries as requirements whenever it is possible. Use new libraries only if strictly needed to solve the issue you are working for. In case of doubt, ask a maintainer permission to use a specific library.
  • If external libraries/packages with restrictive licenses were added, they were added in the Legal Notice section.
  • Linters (Ruff) gave 0 errors. If you have correctly installed pre-commit, it does these checks and adjustments on your behalf.
  • I have added tests for the feature/bug I solved (see tests folder). All the tests (new and old ones) gave 0 errors.
  • If the GUI has been modified:
    • I have a provided a screenshot of the result in the PR.
    • I have created new frontend tests for the new component or updated existing ones.
  • After you had submitted the PR, if DeepSource, Django Doctors or other third-party linters have triggered any alerts during the CI checks, I have solved those alerts.
  • I have addressed raised Copilot issues. In case of FPs, I have commented the Copilot issue and proved that it is wrong before having the comment resolved.
  • I have reviewed and verified any LLM-generated code included in this PR. Also, I have explicitly stated that I have used LLMs in this PR.

Note on LLM assistance: An LLM was used to assist with static code analysis and drafting tests. All changes have been manually reviewed and verified against repository standards.

Copilot AI balanced review requested due to automatic review settings October 3, 2026 11:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI balanced review requested due to automatic review settings October 3, 2026 11:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI balanced review requested due to automatic review settings October 7, 2026 08:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI balanced review requested due to automatic review settings October 7, 2026 16:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@harshitnub077

Copy link
Copy Markdown
Author

Ready for maintainer review whenever convenient. All CI checks pass and the branch is clean and up to date with develop.

@asucodes

Copy link
Copy Markdown

Hi, thanks for the patience and yes, absolutely. Here are the regression tests I promised on #4052, in diff form so you can fold them straight into this branch.

They cover the cases we agreed on: reading a secret organization value as the owner and as an organization admin of that org (both allowed), as a regular member of the org and as a user of another org (both redacted), an owner whose membership was removed after the config was created, an organization config with a null owner that can only appear through bulk writes such as bulk_create, and serializing without a request or with an unauthenticated user. I ran this module green against my local docker stack when I wrote these; since your branch implements the same null-safe logic, they should apply cleanly, though please treat a run on your branch as the source of truth. If anything trips up I will debug it here with you.

Feel free to add me as a co-author when folding them in, and I am happy to review the serializer patch itself while it stays open.

diff --git a/tests/api_app/test_serializers.py b/tests/api_app/test_serializers.py
index 98c6b0ab..89595109 100644
--- a/tests/api_app/test_serializers.py
+++ b/tests/api_app/test_serializers.py
@@ -85,6 +85,113 @@ class PluginConfigSerializerTestCase(CustomTestCase):
         org.delete()
         pc.delete()
 
+    def test_to_representation_secret_for_organization(self):
+        org = Organization.objects.create(name="test_org")
+        other_org = Organization.objects.create(name="other_org")
+        owner = User.objects.create_user(
+            username="config_owner",
+            email="config_owner@intelowl.com",
+            password="test",
+        )
+        Membership.objects.create(user=owner, organization=org, is_owner=True)
+        Membership.objects.create(user=self.admin, organization=org, is_owner=False, is_admin=True)
+        Membership.objects.create(user=self.user, organization=org, is_owner=False, is_admin=False)
+        Membership.objects.create(user=self.guest, organization=other_org, is_owner=True)
+        ac = AnalyzerConfig.objects.get(name="AbuseIPDB")
+        param = Parameter.objects.create(
+            is_secret=True,
+            name="mynewparameter",
+            python_module=ac.python_module,
+            required=True,
+            type="str",
+        )
+        pc = PluginConfig.objects.create(
+            value="supersecret",
+            owner=owner,
+            parameter=param,
+            analyzer_config=ac,
+            for_organization=True,
+        )
+
+        def get_value(user):
+            return PluginConfigSerializer(
+                pc, context={"request": MockUpRequest(user=user)}
+            ).data["value"]
+
+        # the owner and an admin of the owner's organization can read the value
+        self.assertEqual(get_value(owner), "supersecret")
+        self.assertEqual(get_value(self.admin), "supersecret")
+        # other members of the organization and users of other organizations cannot
+        self.assertEqual(get_value(self.user), "redacted")
+        self.assertEqual(get_value(self.guest), "redacted")
+
+        # an owner whose membership was removed after the config was created
+        # (org deleted / member removed) - the realistic way this state occurs
+        orphan_owner = User.objects.create_user(
+            username="orphan_owner",
+            email="orphan_owner@intelowl.com",
+            password="test",
+        )
+        Membership.objects.create(
+            user=orphan_owner,
+            organization=Organization.objects.create(name="orphan_org"),
+            is_owner=True,
+        )
+        pc_no_membership = PluginConfig.objects.create(
+            value="supersecret",
+            owner=orphan_owner,
+            parameter=param,
+            analyzer_config=ac,
+            for_organization=True,
+        )
+        orphan_owner.membership.delete()
+        self.assertEqual(
+            PluginConfigSerializer(
+                pc_no_membership,
+                context={"request": MockUpRequest(user=self.admin)},
+            ).data["value"],
+            "redacted",
+        )
+        # the owner can still read their own value
+        self.assertEqual(
+            PluginConfigSerializer(
+                pc_no_membership,
+                context={"request": MockUpRequest(user=orphan_owner)},
+            ).data["value"],
+            "supersecret",
+        )
+
+        # an owner-less org configuration can only arise through bulk writes
+        # (bulk_create bypasses save/clean) - serializing it must not raise
+        pc_no_owner = PluginConfig.objects.bulk_create(
+            [
+                PluginConfig(
+                    value="supersecret",
+                    owner=None,
+                    parameter=param,
+                    analyzer_config=ac,
+                    for_organization=True,
+                )
+            ]
+        )[0]
+        self.assertEqual(
+            PluginConfigSerializer(
+                pc_no_owner,
+                context={"request": MockUpRequest(user=self.admin)},
+            ).data["value"],
+            "redacted",
+        )
+
+        # missing request or request user also results in a redacted value
+        self.assertEqual(PluginConfigSerializer(pc_no_owner).data["value"], "redacted")
+        self.assertEqual(
+            PluginConfigSerializer(
+                pc_no_owner,
+                context={"request": MockUpRequest(user=None)},
+            ).data["value"],
+            "redacted",
+        )
+
     def test_create(self):
         org = Organization.objects.create(name="test_org")
         m1 = Membership.objects.create(user=self.user, organization=org, is_owner=True)

…uginConfigSerializer secret redaction. Closes intelowlproject#4052

Secret redaction in PluginConfigSerializer dereferenced instance.owner
and the owner's membership without checking them, so a config with no
owner raised AttributeError and a config whose owner no longer has a
membership raised ObjectDoesNotExist, both ending in a 500.

Check the request user, the owner and both memberships before deciding
whether to redact, and compare organization_id instead of loading the
organizations. OwnershipAbstractModel.organization now returns None when
the owner is missing or has no membership, and to_representation falls
back to None for the organization name.

Add regression tests covering secret redaction for organization configs,
bulk-created owner-less configs, membership deletion post-creation,
and missing request context.

Co-authored-by: asucodes <asucodes@users.noreply.github.com>
Signed-off-by: Harshit Kudhial <harshitkudhial@gmail.com>
@harshitnub077
harshitnub077 force-pushed the fix/plugin-config-secret-redaction-null-owner branch from 1e86300 to 6a41065 Compare October 25, 2026 06:07
Copilot AI balanced review requested due to automatic review settings October 25, 2026 06:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@harshitnub077

Copy link
Copy Markdown
Author

Hey @asucodes,

Thanks for providing the test suite! I've added the regression tests for the branch and added you as a co-author for the commit (Co-authored-by: asucodes ).the use of bulk_create to test the owner-less organization configuration without triggering model clean() is a great find! The tests are passing and all linters (ruff) are passing with no errors.

If you get a chance, you can take a look at the updated serializer patch. Thanks for working with me!

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

This pull request has been marked as stale because it has had no activity for 10 days. If you are still working on this, please provide some updates or it will be closed in 5 days.

@github-actions github-actions Bot added the stale label Nov 4, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

This pull request has been closed because it had no updates in 15 days. If you're still working on this fell free to reopen.

@github-actions github-actions Bot closed this Nov 9, 2026
@mlodic mlodic reopened this Nov 13, 2026
@mlodic mlodic added the keep-open To avoid workflow closing PRs label Nov 13, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

keep-open To avoid workflow closing PRs stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants